﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.APP.App_UI.Commons;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_ExpenseRates : System.Web.UI.Page
    {
        double sumBath = 0;
        Main main = new Main();
        IBLL_FIM_MAS_ExpenseRates ExpRate = new B_FIM_MAS_ExpenseRates();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtBgmExpSeq.Text = Request.QueryString["Seq"];
                txtBgmSourceSeq.Text = Request.QueryString["BgmSourceSeq"];
                //SetQty(txtBgmExpSeq.Text);
                //LoadDataRepeaterDtls();
                LoadDataFimExpenseRate();
                cmdOK.OnClientClick = string.Format("returnToParent('{0}', '{1}', '{2}', '{3}', '{4}')", txtSumBathAmount.ClientID
                    , Request.QueryString["txtAmount"], Request.QueryString["txtExchangeRate"], Request.QueryString["txtBathAmount"], Request.QueryString["txGrpAmount"]);
            }

            SetSummary();
        }

        protected void SetSummary()
        {
            double dAmount = 0;
            double dMan = 0;
            double dDay = 0;
            double dHour = 0;
            double dMinute = 0;
            double dBathAmount = 0;
            double dSumBathAmount = 0;

            for (int i = 0; i < grdFimExpenseRate.Items.Count; i++)
            {
                GridDataItem dataItem = grdFimExpenseRate.Items[i];
                RadNumericTextBox txtAmount = (RadNumericTextBox)dataItem["quantity"].FindControl("txtAmount");
                RadNumericTextBox txtMan = (RadNumericTextBox)dataItem["quantity"].FindControl("txtMan");
                RadNumericTextBox txtDay = (RadNumericTextBox)dataItem["quantity"].FindControl("txtDay");
                RadNumericTextBox txtHour = (RadNumericTextBox)dataItem["quantity"].FindControl("txtHour");
                RadNumericTextBox txtMinute = (RadNumericTextBox)dataItem["quantity"].FindControl("txtMinute");
                RadNumericTextBox txtBathAmount = (RadNumericTextBox)dataItem["bath_amount"].FindControl("txtBathAmount");

                if (txtAmount.Text.Length > 0)
                    dAmount = Convert.ToDouble(txtAmount.Text);
                else
                    dAmount = 0;

                if (txtMan.Text.Trim().Length > 0)
                    dMan = Convert.ToDouble(txtMan.Text);
                else
                    dMan = 1;

                if (txtDay.Text.Trim().Length > 0)
                    dDay = Convert.ToDouble(txtDay.Text);
                else
                    dDay = 1;

                if (txtHour.Text.Trim().Length > 0)
                    dHour = Convert.ToDouble(txtHour.Text);
                else
                    dHour = 1;

                if (txtMinute.Text.Trim().Length > 0)
                    dMinute = Convert.ToDouble(txtMinute.Text);
                else
                    dMinute = 1;

                dBathAmount = dAmount * dMan * dDay * dHour * dMinute;
                txtBathAmount.Text = dBathAmount.ToString();
                dSumBathAmount += dBathAmount;
            }

            txtSumBathAmount.Value = dSumBathAmount;
        }

        protected void LoadDataFimExpenseRate()
        {
            string strBgmExpSeq = txtBgmExpSeq.Text;
            string strBgmSourceSeq = txtBgmSourceSeq.Text;
            string strSeq;
            //string[] strExpRateTypeSeq = new string[RepeaterDtls.Items.Count];
            int j = 0;

            //for (int i = 0; i < strExpRateTypeSeq.Length; i++)
            //{
            //    RadComboBox ddl = (RadComboBox)RepeaterDtls.Items[i].FindControl("ddlExpRateTypeSeq");
            //    strSeq = ddl.SelectedValue;

            //    if (strSeq.Length > 0)
            //    {
            //        strExpRateTypeSeq[j] = strSeq;
            //        j++;
            //    }
            //}

            grdFimExpenseRate.DataSource = ExpRate.GetFull(strBgmExpSeq, strBgmSourceSeq);
            grdFimExpenseRate.DataBind();
        }

        //protected void LoadDataRepeaterDtls()
        //{
        //    string strBgmExpSeq = txtBgmExpSeq.Text;

        //    RepeaterDtls.DataSource = ExpRateTypeGrp.GetFull(strBgmExpSeq);
        //    RepeaterDtls.DataBind();
        //}

        //protected void SetQty(string strBgmExpSeq)
        //{
        //    ViewState["bRelate"] = ExpRateType.GetRelate(strBgmExpSeq);
        //}

        //protected void RepeaterDtls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item is RepeaterItem)
        //    {
        //        if (e.Item.ItemType != ListItemType.Header)
        //        {
        //            RepeaterItem dataItem = (RepeaterItem)e.Item;
        //            Label lbl = (Label)dataItem.FindControl("lblExpRateTypeGrpSeq");
        //            RadComboBox ddl = (RadComboBox)dataItem.FindControl("ddlExpRateTypeSeq");

        //            ddl.DataSource = ExpRateType.GetList(lbl.Text);
        //            ddl.DataBind();

        //            main.InsertEmptyDDLAll(ddl);
        //            ddl.SelectedIndex = 0;
        //        }
        //    }
        //}

        protected void ddlExpRateTypeSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDataFimExpenseRate();
        }

        protected void grdFimExpenseRate_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                //tMan, tDay, tHour, tMinute, tAmount, tBathAmount, tAmountMin, tAmountMax
                GridDataItem dataItem = (GridDataItem)e.Item;
                HtmlInputHidden txtAmountMin = (HtmlInputHidden)dataItem["quantity"].FindControl("hAmountMin");
                HtmlInputHidden txtAmountMax = (HtmlInputHidden)dataItem["quantity"].FindControl("hAmountMax");
                RadNumericTextBox txtAmount = (RadNumericTextBox)dataItem["quantity"].FindControl("txtAmount");
                RadNumericTextBox txtMan = (RadNumericTextBox)dataItem["quantity"].FindControl("txtMan");
                RadNumericTextBox txtDay = (RadNumericTextBox)dataItem["quantity"].FindControl("txtDay");
                RadNumericTextBox txtHour = (RadNumericTextBox)dataItem["quantity"].FindControl("txtHour");
                RadNumericTextBox txtMinute = (RadNumericTextBox)dataItem["quantity"].FindControl("txtMinute");
                RadNumericTextBox txtBathAmount = (RadNumericTextBox)dataItem["bath_amount"].FindControl("txtBathAmount");
                Label lblMan = (Label)dataItem["quantity"].FindControl("lblMan");
                Label lblDay = (Label)dataItem["quantity"].FindControl("lblDay");
                Label lblHour = (Label)dataItem["quantity"].FindControl("lblHour");
                Label lblMinute = (Label)dataItem["quantity"].FindControl("lblMinute");
                bool[] bRelate = (bool[])ViewState["bRelate"];

                txtAmount.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
                    , txtMan.ClientID, txtDay.ClientID, txtHour.ClientID, txtMinute.ClientID
                    , txtAmount.ClientID, txtBathAmount.ClientID
                    , txtAmountMin.ClientID, txtAmountMax.ClientID));
                txtAmount.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}')"
                    , txtAmount.ClientID, txtBathAmount.ClientID));

                if (bRelate[0])
                {
                    txtMan.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
                        , txtMan.ClientID, txtDay.ClientID, txtHour.ClientID, txtMinute.ClientID
                        , txtAmount.ClientID, txtBathAmount.ClientID
                        , txtAmountMin.ClientID, txtAmountMax.ClientID));
                    txtMan.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}')"
                        , txtAmount.ClientID, txtBathAmount.ClientID));
                    txtMan.Visible = true;
                    lblMan.Visible = true;
                }

                if (bRelate[1])
                {
                    txtDay.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
                        , txtMan.ClientID, txtDay.ClientID, txtHour.ClientID, txtMinute.ClientID
                        , txtAmount.ClientID, txtBathAmount.ClientID
                        , txtAmountMin.ClientID, txtAmountMax.ClientID));
                    txtDay.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}')"
                        , txtAmount.ClientID, txtBathAmount.ClientID));
                    txtDay.Visible = true;
                    lblDay.Visible = true;
                }

                if (bRelate[2])
                {
                    txtHour.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
                        , txtMan.ClientID, txtDay.ClientID, txtHour.ClientID, txtMinute.ClientID
                        , txtAmount.ClientID, txtBathAmount.ClientID
                        , txtAmountMin.ClientID, txtAmountMax.ClientID));
                    txtHour.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}')"
                        , txtAmount.ClientID, txtBathAmount.ClientID));
                    txtHour.Visible = true;
                    lblHour.Visible = true;
                }

                if (bRelate[3])
                {
                    txtMinute.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}')"
                        , txtMan.ClientID, txtDay.ClientID, txtHour.ClientID, txtMinute.ClientID
                        , txtAmount.ClientID, txtBathAmount.ClientID
                        , txtAmountMin.ClientID, txtAmountMax.ClientID));
                    txtMinute.Attributes.Add("OnFocus", string.Format("txtFocus('{0}', '{1}')"
                        , txtAmount.ClientID, txtBathAmount.ClientID));
                    txtMinute.Visible = true;
                    lblMinute.Visible = true;
                }
                txtMan.Text = "1";
                txtDay.Text = "1";
                txtHour.Text = "1";
                txtMinute.Text = "1";

                string strBathAmount = txtBathAmount.Text;
                if (strBathAmount.Length == 0)
                    strBathAmount = "0";
                sumBath += double.Parse(strBathAmount);
            }
        }
    }
}
